package com.xqboss.apps.service.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.equipment.EquipmentUpgradeRecord;
import com.xqboss.apps.dto.equipment.WebEquipmentPreUpgradeRecordDto;
import com.xqboss.apps.enums.equipment.EquipmentUpgradeRecordTypeEnum;
import com.xqboss.apps.mapper.equipment.EquipmentUpgradeRecordMapper;
import com.xqboss.apps.vo.equipment.WebEquipmentUpgradeRecordVo;
import com.xqboss.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 装备升级记录
 */
@Service
public class EquipmentUpgradeRecordService extends ServiceImpl<EquipmentUpgradeRecordMapper, EquipmentUpgradeRecord> {

    /**
     * 保存升级记录
     * @param record
     */
    public void saveRecord(EquipmentUpgradeRecord record){
        boolean save = save(record);
        if(!save){
            throw new ServiceException("操作失败！");
        }
    }

    public List<WebEquipmentUpgradeRecordVo> upgradeRecord(WebEquipmentPreUpgradeRecordDto params) {
        return baseMapper.upgradeRecord(params);
    }

    /**
     * 根据用户id，装备id，获取装备对应等级 突破的次数
     * 【突破次数】：当前装备当前等级是第几次突破
     */
    public long getBreakCountByUserIdAndEquipmentId(Long userId, Long equipmentId, Integer level) {
        LambdaQueryWrapper<EquipmentUpgradeRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EquipmentUpgradeRecord::getUserId, userId);
        wrapper.eq(EquipmentUpgradeRecord::getEquipmentId, equipmentId);
        wrapper.eq(EquipmentUpgradeRecord::getLevelBefore, level);
        wrapper.eq(EquipmentUpgradeRecord::getType, EquipmentUpgradeRecordTypeEnum.BREAK);
        Long breakCount = baseMapper.selectCount(wrapper);
        return breakCount == null ? 0 : breakCount;
    }
}
